home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / answers / rec / puzzles / archive / combinatorics < prev    next >
Text File  |  1993-08-17  |  22KB  |  600 lines

  1. Newsgroups: rec.puzzles,news.answers,rec.answers
  2. Path: senator-bedfellow.mit.edu!bloom-beacon.mit.edu!spool.mu.edu!howland.reston.ans.net!europa.eng.gtefsd.com!uunet!questrel!chris
  3. From: chris@questrel.com (Chris Cole)
  4. Subject: rec.puzzles Archive (combinatorics), part 05 of 35
  5. Message-ID: <puzzles/archive/combinatorics_745653851@questrel.com>
  6. Followup-To: rec.puzzles
  7. Summary: This is part of an archive of questions
  8.  and answers that may be of interest to
  9.  puzzle enthusiasts.
  10.  Part 1 contains the index to the archive.
  11.  Read the rec.puzzles FAQ for more information.
  12. Sender: chris@questrel.com (Chris Cole)
  13. Reply-To: archive-comment@questrel.com
  14. Organization: Questrel, Inc.
  15. References: <puzzles/archive/Instructions_745653851@questrel.com>
  16. Date: Wed, 18 Aug 1993 06:04:32 GMT
  17. Approved: news-answers-request@MIT.Edu
  18. Expires: Thu, 1 Sep 1994 06:04:11 GMT
  19. Lines: 578
  20. Xref: senator-bedfellow.mit.edu rec.puzzles:24991 news.answers:11511 rec.answers:1911
  21.  
  22. Archive-name: puzzles/archive/combinatorics
  23. Last-modified: 17 Aug 1993
  24. Version: 4
  25.  
  26.  
  27. ==> combinatorics/alphabet.blocks.p <==
  28. What is the minimum number of dice painted with one letter on all six sides
  29. such that all permutations without repetitions of n letters can be formed
  30. by placing n dice together in a line?
  31.  
  32. ==> combinatorics/alphabet.blocks.s <==
  33. n=   2          3          4          5          6
  34.    (8,4)      (9,7)      (9,3)      (10,7)     (11,7)
  35.  
  36.    aijklm     abcde?     acdefg     abcde?     abkmuz
  37.    bijklm     fghij?     bhijkl     fghij?     bcpwy?
  38.    cnopqr     klmno?     cmnopq     klmno?     cdlnvz
  39.    dnopqr     pqrst?     dhnrvy     pqrst?     deqxu?
  40.    estuvw     uvwxy?     eiosvw     uvwxy?     efmowz
  41.    fstuvw     afkpu?     fjptwx     afkpu?     fgryv?
  42.    gxyz??     bglqv?     gkquxy     bglqv?     ghnkx?
  43.    hxyz??     chmrwz     lmrstu     chmrwz     hisuw?
  44.               dinsxz     zab???     dinsxz     ijoly?
  45.                                     ejotyz     jatvx?
  46.                                                pqrstz
  47.  
  48. I think I can prove that there is no solution with 11 dice with 9
  49. don't cares or with 10 dice, but I haven't checked all the details, so
  50. I might have made a mistake.  In any case, that leaves open the case
  51. of 11 dice with 8 don't cares; my guess is that it is not possible.
  52.  
  53. -- John Rickard (jrickard@eoe.co.uk)
  54.  
  55. ==> combinatorics/coinage/combinations.p <==
  56. Assuming you have enough coins of 1, 5, 10, 25 and 50 cents, how many
  57. ways are there to make change for a dollar?
  58.  
  59. ==> combinatorics/coinage/combinations.s <==
  60. 292.  The table is shown below:
  61.  
  62. Amount  00 05 10 15 20 25 30 35 40 45 50 55 60 65  70  75  80  85  90  95  100
  63. Coins  
  64. .01      1  1  1  1  1  1  1  1  1  1  1  1  1  1   1   1   1   1   1   1   1
  65. .05      1  2  3  4  5  6  7  8  9 10 11 12 13 14  15  16  17  18  19  20  21
  66. .10      1  2  4  6  9 12 16 20 25 30 36 42 49 56  64  72  81  90 100 110 121
  67. .25      1  2  4  6  9 13 18 24 31 39 49 60 73 87 103 121 141 163 187 214 242
  68. .50      1  2  4  6  9 13 18 24 31 39 49 62 77 93 112 134 159 187 218 253 292
  69.  
  70. The meaning of each entry is as follows:
  71. If you wish to make change for 50 cents using only pennies, nickels and dimes,
  72. go to the .10 row and the 50 column to obtain 36 ways to do this.
  73.  
  74. To calculate each entry, you start with the pennies.  There is exactly one
  75. way to make change for every amount.  Then calculate the .05 row by adding 
  76. the number of ways to make change for the amount using pennies plus the number
  77. of ways to make change for five cents less using nickels and pennies.  This 
  78. continues on for all denominations of coins.  
  79.  
  80. An example, to get change for 75 cents using all coins up to a .50, add the
  81. number of ways to make change using only .25 and down (121) and the number of
  82. ways to make change for 25 cents using coins up to .50 (13).  This yields the
  83. answer of 134.
  84.  
  85. ==> combinatorics/coinage/dimes.p <==
  86. "Dad wants one-cent, two-cent, three-cent, five-cent, and ten-cent
  87. stamps.  He said to get four each of two sorts and three each of the
  88. others, but I've forgotten which.  He gave me exactly enough to buy
  89. them; just these dimes."  How many stamps of each type does Dad want?
  90. A dime is worth ten cents.  -- J.A.H. Hunter
  91.  
  92. ==> combinatorics/coinage/dimes.s <==
  93. The easy way to solve this is to sell her three each, for
  94. 3x(1+2+3+5+10) = 63 cents.  Two more stamps must be bought, and they
  95. must make seven cents (since 17 is too much), so the fourth stamps are
  96. a two and a five.
  97.  
  98. ==> combinatorics/coinage/impossible.p <==
  99. What is the smallest number of coins that you can't make a dollar with?
  100. I.e., for what N does there not exist a set of N coins adding up to a dollar?
  101. It is possible to make a dollar with 1 current U.S. coin (a Susan B. Anthony),
  102. 2 coins (2 fifty cent pieces), 3 coins (2 quarters and a fifty cent piece),
  103. etc.  It is not possible to make exactly a dollar with 101 coins.
  104.  
  105. ==> combinatorics/coinage/impossible.s <==
  106. The answer is 77:
  107.  
  108. a) 5c  = 1 or 5;
  109. b) 10c = 1 or 2 a's (1,2,6,10)
  110. c) 25c = 1 or 2 b's + 1 a
  111. d) 50c = 1 or 2 c's
  112. e) $1  = 1 or 2 d's
  113.  
  114. total    penny    nickel    dime    quarter    half
  115. 5        1    2    1    1
  116. 6        3    1    1    1
  117. 7        5        1    1
  118. 8        4    3        1
  119. 9        6    2        1
  120. 10        8    1        1
  121. 11        10            1
  122. 12        7    4    1
  123. 13        9    3    1
  124. 14        11    2    1
  125. 15        13    1    1
  126. 16        15        1
  127. 17        14    3
  128. 18        16    2
  129. 19        18    1
  130. 20        20
  131. 21    5    13    3
  132. 22    5    15    2
  133. 23    5    17    1
  134. 24    5    19
  135. 25    10    12    3
  136. 26    10    14    2
  137. 27    10    16    1
  138. 28    10    18
  139. 29    15    11    3
  140. 30    15    13    2
  141. 31    15    15    1
  142. 32    15    17
  143. 33    20    10    3
  144. 34    20    12    2
  145. 35    20    14    1
  146. 36    20    16
  147. 37    25    9    3
  148. 38    25    11    2
  149. 39    25    13    1
  150. 40    25    15
  151. 41    30    8    3
  152. 42    30    10    2
  153. 43    30    12    1
  154. 44    30    14
  155. 45    35    7    3
  156. 46    35    9    2
  157. 47    35    11    1
  158. 48    35    13
  159. 49    40    6    3
  160. 50    40    8    2
  161. 51    40    10    1
  162. 52    40    12
  163. 53    45    5    3
  164. 54    45    7    2
  165. 55    45    9    1
  166. 56    45    11
  167. 57    50    4    3
  168. 58    50    6    2
  169. 59    50    8    1
  170. 60    50    10
  171. 61    55    3    3
  172. 62    55    5    2
  173. 63    55    7    1
  174. 64    55    9
  175. 65    60    2    3
  176. 66    60    4    2
  177. 67    60    6    1
  178. 68    60    8
  179. 69    65    1    3
  180. 70    65    3    2
  181. 71    65    5    1
  182. 72    65    7
  183. 73    70        3
  184. 74    70    2    2
  185. 75    70    4    1
  186. 76    70    6
  187. 77    can't be done
  188. 78    75    1    2
  189. 79    75    3    1
  190. 80    75    5
  191. 81    can't be done
  192. 82    80        2
  193. 83    80    2    1
  194. 84    80    4    
  195. 85    can't be done
  196. 86    can't be done
  197. 87    85    1    1
  198. 88    85    3    
  199. 89    can't be done
  200. 90    can't be done
  201. 91    90        1
  202. 92    90    2
  203. 93-95    can't be done
  204. 96    95    1
  205. 97-99    can't be done
  206. 100    100
  207.  
  208. ==> combinatorics/color.p <==
  209. An urn contains n balls of different colors.  Randomly select a pair, repaint
  210. the first to match the second, and replace the pair in the urn.  What is the
  211. expected time until the balls are all the same color?
  212.  
  213. ==> combinatorics/color.s <==
  214. (n-1)^2.
  215.  
  216. If the color classes have sizes k1, k2, ..., km, then the expected number of
  217. steps from here is (dropping the subscript on k):
  218.  
  219.      2               k(k-1)              (j-1) (k-j)
  220. (n-1)  -  SUM      ( ------  +   SUM   --------------- )
  221.          classes,      2        1<j<k      (n-j)
  222.       class.size=k
  223.  
  224. The verification goes roughly as follows.  Defining  phi(k) as  (k(k-1)/2 +
  225. sum[j]...), we first show that  phi(k+1) + phi(k-1) - 2*phi(k) = (n-1)/(n-k)
  226. except when k=n; the k(k-1)/2 contributes 1, the term j=k contributes
  227. (j-1)/(n-j)=(k-1)/(n-k), and the other summands j<k contribute nothing.
  228. Then we say that the expected change in phi(k) on a given color class is
  229. k*(n-k)/(n*(n-1)) times (phi(k+1) + phi(k-1) - 2*phi(k)), since with
  230. probability k*(n-k)/(n*(n-1)) the class goes to size k+1 and with the same
  231. probability it goes to size k-1.  This expected change comes out to k/n.
  232. Summing over the color classes (and remembering the minus sign), the
  233. expected change in the "cost from here" on one step is -1, except when we're
  234. already monochromatic, where the handy exception k=n kicks in.
  235.  
  236. One can rewrite the contribution from k as
  237.  
  238.    (n-1) SUM (k-j)/(n-j)
  239.         0<j<k
  240.  
  241. which incorporates both the k(k-1)/2 and the previous sum over j.
  242. That makes the proof a little cleaner.
  243.  
  244. ==> combinatorics/full.p <==
  245. Consider a string that contains all substrings of length n.  For example,
  246. for binary strings with n=2, a shortest string is 00110 -- it contains 00,
  247. 01, 10 and 11 as substrings.  Find the shortest such strings for all n.
  248.  
  249. ==> combinatorics/full.s <==
  250. Knuth, Volume 2 Seminumerical Algorithms, section 3.2.2 discusses this problem.
  251. He cites the following results:
  252. Shortest length: m^n + n - 1, where m = number of symbols in the language.
  253. Algorithms:
  254. [Exercise 7, W. Mantel, 1897]
  255. The binary sequence is the LSB of X computed by the MIX program:
  256.     LDA X
  257.     JANZ *+2
  258.     LDA A
  259.     ADD X
  260.     JNOV *+3
  261.     JAZ *+2
  262.     XOR A
  263.     STA X
  264. [Exercise 10, M. H. Martin, 1934]
  265. Set x[1] = x[2] = ... = x[n] = 0.  Set x[i+1] = largest value < n such that
  266. substring of n digits ending at x[i+1] does not occur earlier in string.
  267. Terminate when this is not possible.
  268.  
  269. If we instead consider the strings as circular, we have a well known
  270. problem whose solution is given by any hamiltonian cycle in the de
  271. Bruijn (or Good) graph of dimension K.  (Or equivalently an eulerian
  272. circuit in the de Bruijn graph of dimension K-1) As a string of length
  273. 2^K is produced, it must be optimal, and any shortest sequence must be
  274. an eulerian circuit in a dB graph.
  275.  
  276. The de Bruijn graph Tn has as its vertex set the binary n-strings.
  277. Directed edges join n-strings that may be derived by deleting the left
  278. most digit and appending a 0 or 1 to the right end.  de Bruijn + van
  279. Ardenne-Ehrenfest (in 1951) counted the number of eulerian circuits in
  280. Tn. There are 2^(2^(n-1)-n) of them.
  281.  
  282. Some examples:
  283. K=2 1100
  284. K=3 11101000
  285. K=4 1111001011010000
  286.  
  287. The solution to the above problem (non-circular strings) can be found
  288. by duplicating the first K-1 digits of the solution string at the end
  289. of the string.  These are not the only solutions, but they
  290. are of minimum length: 2^K + K-1.  
  291.  
  292. We can obtain a lower bound for the optimal sequence for the general case as
  293. follows:
  294.  
  295. Consider first the simpler case of breaking into an answer machine which
  296. accepts d+1 digits, values 0 to n-1.  We wish to find the minimal universal
  297. code that will allow us access to any such answering machine.
  298.  
  299. Let us construct a digraph G = (V,E), where the n^d vertices are labelled
  300. with a d sequence of digits.  Notation: let [v_{i,1},v_{i,2},...,v_{i,d}]
  301. denote the labelling on node v_i.  An edge e = (v_i, v_j) is in E iff for k
  302. in 1, ..., d-1: v_{i,k+1} = v_{j,k}, i.e., the last d-1 digits in the
  303. labelling of the initial vertex of e is identical with the first d-1 digits
  304. in the labelling of the terminal vertex of e.  We associate with each edge a
  305. value, t(e) = v_{j,d}, the last digit in the labelling of the terminal
  306. vertex.
  307.  
  308. The intuition goes as follows:  we are going to perform a Euler circuit of
  309. the digraph, where the label on the current vertex gives the last d digits
  310. in the output sequence so far.  If we make a transition on edge e, we output
  311. the tone/digit t(e) as the next output value, thus preserving the invariant
  312. on the labelling.
  313.  
  314. How do we know that a Euler circuit exists?  Simple:  a connected digraph
  315. has an Euler circuit iff for all vertices v: indegree(v) = outdegree(v).
  316. This property is trivially true for this digraph.
  317.  
  318. So, in order to generate a universal code for the AM, we simply output 0^d
  319. (to satisfy the precondition for being in vertex [0,...,0]), and perform an
  320. Euler circuit starting at node [0,...,0].
  321.  
  322. Now, the total length of the universal sequence is just the number of edges
  323. traversed in the Euler circuit plus the initial precondition sequence, or
  324. n^d * n + d (number of vertices times the out-degree) or n^{d+1} + d.  That
  325. this is a minimal sequence is obvious.
  326.  
  327. Next, let us consider the machine AM' where the security code is of the form
  328. [0...n-1]^d [0...m-1], i.e., d digits ranging from 0 to n-1, followed by a
  329. terminal digit ranging from 0 to m-1, m < n.
  330.  
  331. We build a digraph G = (V, E) similar to the construction above, except for
  332. the following:  an edge e is in E iff t(e) in 0 to m-1.  This digraph is
  333. clearly non-Eulerian.  In particular, there are two classes of vertices:
  334.  
  335. (1) v is of the form [0...n-1]^{d-1} [0...m-1]  (``fat'' vertices)
  336.  
  337.     and
  338.  
  339. (2) v is of the form [0...n-1]^{d-1} [m...n-1]  (``thin'' vertices)
  340.  
  341. Observations:  there are (n^{d-1} * m) fat vertices, and (n^{d-1} * (n-m))
  342. thin vertices.  All vertices have out-degree of m.  Fat vertices have
  343. in-degrees of n, and thin vertices have in-degrees of 0.  Color all the
  344. edges blue.
  345.  
  346. The question now becomes:  can we put a bound on how many new (red) edges
  347. must we add to G in order to make a blue edge covering path possible?
  348. (Instead of thinking of edges being traversed multiple times in the blue
  349. edge covering path, we allow multiple edges between vertices and allow each
  350. edge to be traversed once.)  Note that, in this procedure, we add edges only
  351. if it is allowed (the vertex labelling constraint).  We will first obtain a
  352. lower bound on the length of a blue covering circuit, and then transform it
  353. into a bound for arbitrary blue covering paths.
  354.  
  355. Clearly, we must add at least (n-m)*(n^{d-1}*m) edges incident from the fat
  356. vertices.  [ We need (n-m) new out-going edges for each of (n^{d-1}*m)
  357. vertices to bring the out-degree up to the in-degree. ]
  358.  
  359. Let us partition our vertices into sets.  Denote the range [0..m-1] by S,
  360. the range [m..n-1] by L, and the range [0..n-1] by X.
  361.  
  362. Let S_0 = { v: v = [X^{d-1}S] }.  S_0 is just the set of fat vertices.
  363. Define in(S_0) = number of edges from vertices not in S to vertices in S.
  364. Define out(S_0) in the corresponding fashion, and let excess(S_0) =
  365. in(S_0)-out(S_0).  Clearly, excess(S_0) = n^{d-1}m(n-m) from the argument
  366. above.  Generalizing the requirement for Eulerian digraphs, we see that we
  367. must add excess(S_0) edges from S_0 if the blue edges connected to/within
  368. S_0 are to be covered by some circuit (edges may not be travered multiple
  369. times -- we add parallel edges to handle that case).  In particular, edges
  370. from S_0 will be incident on vertices of the form [X^{d-2}SX].  Furthermore,
  371. they can not be [X^{d-2}SS] since that is a subset of S_0 and adding those
  372. edges will not help excess(S_0).  [Now, these edges may be needed if we are
  373. to have a circuit, but we do not consider them since they do not help
  374. excess(S_0).] So, we are forced to add excess(S_0) edges from S_0 to S_1 = {
  375. v: v = [X^{d-2}SL] }.  Color these newly added edges red.
  376.  
  377. Let us define in(S_1), out(S_1) and excess(S_1) as above for the modified
  378. digraph, i.e., including the red excess(S_0) edges that we just added.
  379. Clearly, in(S_1) = out(S_0) = n^{d-1}m(n-m), and out(S_1) = m*|S_1| =
  380. m*n^{d-2}m(n-m), so excess(S_1) = n^{d-2}m(n-m)^2.  Consider S_0 union S_1.
  381. We must add excess(S_1) edges to S_0 union S_1 to make it possible for the
  382. digraph to be covered by a circuit, and these edges must go from {S_0 union
  383. S_1} to S_2 = { v: v = [X^{d-3}SL^2] } by a similar argument as before.
  384. Repeating this partitioning process, eventually we get to S_{d-1} = { v: v =
  385. [SL^{d-1}] }, where union of S_0 to S_{d-1} will need edges to S_d = { v: v
  386. = [L^d] }, where this process terminates.  Note that at this time,
  387. excess(union of S_0 to S_{d-1}) = m(n-m)^d, but in(S_d) = 0 and out(S_d) =
  388. m(n-m)^d, and the process terminates.
  389.  
  390. What have we shown?  Adding up blue edges and the red edges gives us a lower
  391. bound on the total number of edges in a blue-edges covering circuit (not
  392. necessarily Eulerian) in the complete digraph.  This comes out to be
  393. n^{d+1}-(n-m)^{d+1} edges.
  394.  
  395. Next, we note that if we had an optimal path covering all the blue edges, we
  396. can transform it into a circuit by adding d edges.  So, a minimal path can
  397. be no more than d edges shorter than the minimal circuit covering all blue
  398. edges.  [Otherwise, we add d extra edges to make it into a shorter circuit.]
  399.  
  400. So the shortest blue covering path through the digraph is at least
  401. n^{d+1}-{n-m}^{d+1}-d.  With an initial pre-condition sequence of length d
  402. (to establish the transition invariant), the shortest universal answering
  403. machine sequence is of length at least n^{d+1}-(n-m)^{d+1}.
  404.  
  405. While this has not been that constructive, it is easy to see that we can
  406. achieve this bound.  If we looked at the vertices in each of the S_i's, we
  407. just add exactly the edges to S_{i+1} and no more.  The resultant digraph
  408. would be Eulerian, and to find the minimal path we need only start at the
  409. vertex labelled [{n-1}^d], find the Euler circuit, and omit the last d edges
  410. from the tour.
  411.  
  412. ==> combinatorics/gossip.p <==
  413. n people each know a different piece of gossip.  They can telephone each other
  414. and exchange all the information they know (so that after the call they both
  415. know anything that either of them knew before the call).  What is the smallest
  416. number of calls needed so that everyone knows everything?
  417.  
  418. ==> combinatorics/gossip.s <==
  419. 1 for n=2
  420. 3 for n=3
  421. 2n-4 for n>=4
  422.  
  423. This can be achieved as follows: choose four people (A, B, C, and D) as
  424. the "core group".  Each person outside the core group phones a member of
  425. the core group (it doesn't matter which); this takes n-4 calls.  Now the
  426. core group makes 4 calls: A-B, C-D, A-C, and B-D.  At this point, each
  427. member of the core group knows everything.  Now, each person outside the
  428. core group calls anybody who knows everything; this again requires n-4
  429. calls, for a total of 2n-4.
  430.  
  431. The solution to the "gossip problem" has been published several times:
  432.  
  433.     1.  R. Tidjeman, "On a telephone problem", Nieuw Arch. Wisk. 3
  434.         (1971), 188-192.
  435.  
  436.     2.  B. Baker and R. Shostak, "Gossips and telephones", Discrete
  437.         Math. 2 (1972), 191-193.
  438.  
  439.     3.  A. Hajnal, E. C. Milner, and E. Szemeredi, "A cure for the
  440.         telephone disease", Canad Math. Bull 15 (1976), 447-450.
  441.  
  442.     4. Kleitman and Shearer, Disc. Math. 30 (1980), 151-156.
  443.  
  444.     5.  R. T. Bumby, "A problem with telephones", Siam J. Disc. Meth. 2
  445.         (1981), 13-18.
  446.  
  447. ==> combinatorics/grid.dissection.p <==
  448. How many (possibly overlapping) squares are in an mxn grid?  Assume that all
  449. the squares have their edges parallel to the edges of the grid.
  450.  
  451. ==> combinatorics/grid.dissection.s <==
  452. Given an n*m grid with n > m.
  453.  
  454. Orient the grid so n is its width.  Divide the grid into two portions, 
  455. an m*m square on the left and an (n-m)*m rectangle on the right.
  456. Count the squares that have their upper right-hand corners in the
  457. m*m square.  There are m^2 of size 1*1, (m-1)^2 of size 2*2, ...
  458. up to 1^2 of size m*m.  Now look at the n-m columns of lattice points
  459. in the rectangle on the right, in which we find upper right-hand
  460. corners of squares not yet counted.  For each column we count m new
  461. 1*1 squares, m-1 new 2*2 squares, ... up to 1 new m*m square.
  462.  
  463. Combining all these counts in summations:
  464.  
  465.      m           m
  466. total = sum i^2 + (n - m) sum i
  467.     i=1          i=1
  468.  
  469.     (2m + 1)(m + 1)m   (n - m)(m + 1)m
  470.       = ---------------- + ---------------
  471.         6          2
  472.  
  473.       = (3n - m + 1)(m + 1)m/6
  474.  
  475. -- David Karr
  476.  
  477. ==> combinatorics/permutation.p <==
  478. Compute the nth permutation of k numbers (or objects).
  479.  
  480. ==> combinatorics/permutation.s <==
  481. #include <stdio.h>
  482.  
  483. /*
  484. adapted from 'Notation as a Tool of Thought', by K.E.Iverson
  485.  
  486. Radix Representation of Permutations
  487. */
  488.  
  489. /* direct from radix; of given order */
  490. dfr(short direct[],short radix[],long order)
  491. {
  492.     if (order)
  493.     {
  494.         direct[0] = radix[0];
  495.         dfr (direct+1, radix+1, order-1);
  496.         while (--order)
  497.             direct[order] += direct[order] >= direct[0];
  498.     }
  499. }
  500.  
  501. /* radix representation; of given order and given index */
  502. rr(short radix[], long order, long index)
  503. {
  504.     int i;
  505.     
  506.     for (i=1; i<=order; i++)
  507.     {
  508.         radix[order-i] = index % i;
  509.         index = index/i;
  510.     }
  511. }
  512.  
  513. show(short perm[],long order)
  514. {
  515.     while(order--)
  516.         printf("%hd ",*perm++);
  517.     printf("\n");
  518. }
  519.  
  520.  
  521. short parity(short radix[],long order)
  522. {
  523.     long p=0;
  524.     
  525.     while(order--)
  526.         p+=*radix++;
  527.     return p%2;
  528. }
  529.  
  530. void usage(char *name)
  531. {
  532.     fprintf(stderr,"usage: %s order number_of_permutation\n",name);
  533.     exit(-1);
  534. }
  535.  
  536. main(int argc, char *argv[])
  537. {
  538. #define MAX_ORDER    512
  539.     short radix[MAX_ORDER], direct[MAX_ORDER];
  540.     long order, nth;
  541.     
  542.     if (argc!=3) usage(argv[0]);
  543.     order = atol(argv[1]);
  544.     nth = atol(argv[2]);
  545.     rr(radix, order, nth-1);    /* where 0 is the first permuatation */
  546.     dfr(direct, radix, order);
  547.     
  548.     printf("radix  "); show(radix,order);
  549.     printf("direct "); show(direct,order);
  550.     printf("parity %d\n",parity(radix,order));
  551. }
  552.  
  553.  
  554. -- 
  555. J. Henri Schueler, H&h Software, Toronto    +1 416 698 9075 
  556. jhs@ipsa.reuter.com
  557.  
  558. ==> combinatorics/subsets.p <==
  559. Out of the set of integers 1,...,100 you are given ten different
  560. integers.  From this set, A, of ten integers you can always find two
  561. disjoint non-empty subsets, S & T, such that the sum of elements in S
  562. equals the sum of elements in T.  Note: S union T need not be all ten
  563. elements of A.  Prove this.
  564.  
  565. ==> combinatorics/subsets.s <==
  566. There are 2^10 = 1,024 subsets of the 10 integers, but there can be only 901
  567. possible sums, the number of integers between the minimum and maximum sums.
  568. With more subsets than possible sums, there must exist at least one sum that
  569. corresponds to at least two subsets.  Call two subsets with equal sums A and B.
  570. Let C = A intersect B; define S = A - C, T = B - C.  Then S is disjoint from T,
  571. and sum(S) = sum(A-C) = sum(A) - sum(C) = sum(B) - sum(C) = sum(B-C) = sum(T).
  572. QED
  573.  
  574. Addendum: 9 integers suffice.  This was part of my Westinghouse project
  575. in 1981 (the above problem was in Martin Gardner's Scientific American
  576. column not long before).  The argument is along the same lines, but
  577. a bit more complicated; for starters you only work with the subsets
  578. consisting of 3, 4, 5, or 6 of the 9 elements.
  579.  
  580. Let M(n) be the smallest integer such that there exists an n-element
  581. set {a1,a2,a3,...,an=M(n)} of positive integers all 2^n of whose
  582. subsums are distinct.  The pigeonhole argument of subsets.s shows that
  583. M(n)>2^n/n, and it is known that M(n)>c*2^n/sqrt(n) for some c>0.
  584. It is still an unsolved problem (with an Erdos bounty) whether
  585. there is a positive constant c such that M(n)>c*2^n for all n.
  586.  
  587. --Noam D. Elkies (elkies@zariski.harvard.edu)
  588.   Dept. of Mathematics, Harvard University
  589.  
  590. ==> combinatorics/transitions.p <==
  591. How many n-bit binary strings (0/1) have exactly k transitions
  592. (an adjacent pair of dissimilar bits, i.e., a 01 or a 10)?
  593.  
  594. ==> combinatorics/transitions.s <==
  595. A transition can occur at an adjacent pair (i,i+1) where 1<=i<i+1<=n.
  596. Since there are k transitions, there are C(n-1,k) total number of ways
  597. that transitions can occur.  But the string may start with a 1 or a 0
  598. (after which its transitions uniquely determine the string).  So there
  599. are a total of 2C(n-1,k) such strings.
  600.